
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 *
 */

package com.hlkj.pay.app.token.ext.impl;

import org.springframework.stereotype.Service;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.common.IAuthCodeService;
import com.hlkj.pay.app.token.ext.AuthTokenExtPt;
import com.hlkj.pay.common.TenantSecretContext;
import com.hlkj.pay.common.constants.TenantAuthConstant;
import com.hlkj.pay.dto.admin.AdminUserDto;
import com.hlkj.pay.enums.AdminUserEnum;
import com.hlkj.pay.enums.AuthTokenEnum;
import com.hlkj.pay.infrastructure.model.token.TokenSecretDO;
import com.hlkj.pay.service.admin.IAdminUserService;
import com.hlkj.pay.service.token.impl.TokenCommonService;
import com.hlkj.pay.vo.common.req.token.AuthTokenClientRequest;
import com.hlkj.pay.vo.common.req.token.PhoneTokenRequest;
import com.hlkj.pay.vo.common.resp.token.OauthTokenResp;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/09/30 10:42
 */
@Slf4j
@Service
@Extension(bizId = TenantAuthConstant.ADMIN_PHONE_AUTH_CODE)
@RequiredArgsConstructor
public class AdminPhoneAuthCodeTokenServiceExt extends AdminLoginCommonCheck implements AuthTokenExtPt {
    private final IAdminUserService adminUserService;
    private final IAuthCodeService authCodeService;
    private  final TokenCommonService tokenCommonService;


    /**
     * OauthTokenResp
     * 
     * @return
     */
    @Override
    public CommonResult<OauthTokenResp> oauthToken(AuthTokenClientRequest authTokenClientRequest) {
        PhoneTokenRequest phoneTokenRequest = (PhoneTokenRequest) authTokenClientRequest;
        CommonResult<Void> voidCommonResult = authCodeService.checkAuthCode(phoneTokenRequest.getPhone(),
                AdminUserEnum.AdminAuthCodeEnum.ADMIN_AUTH_CODE_LOGIN, phoneTokenRequest.getSmsCode());
        if(!voidCommonResult.isSuccess()){
            return CommonResult.error(voidCommonResult.getCode(),voidCommonResult.getMsg());
        }
        TokenSecretDO tenantSecretDO = TenantSecretContext.get();
        AdminUserDto adminUserDto=null;
        try {
            adminUserDto = adminUserService.queryUserByPhone(phoneTokenRequest.getPhone(),null);
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
        voidCommonResult = checkUser(tenantSecretDO, adminUserDto);
        if(!voidCommonResult.isSuccess()){
            return CommonResult.error(voidCommonResult.getCode(),voidCommonResult.getMsg());
        }
        OauthTokenResp oauthTokenResp;
        AuthTokenEnum.AUTH_TOKEN_REQUEST_TYPE authTokenClientType = AuthTokenEnum.AUTH_TOKEN_REQUEST_TYPE.PHONE;
        // 多因子认证
        // if (AdminUserEnum.YES_NO_TYPE.YES.getCode().equals(adminUserDto.getMfaFlag())) {
        //     oauthTokenResp = tokenCommonService.mfaAccessToken(tenantSecretDO, adminUserDto.getId());
        //     authTokenClientType = AuthTokenEnum.AUTH_TOKEN_REQUEST_TYPE.MFA;
        // }
        // else {
            // 普通认证
            oauthTokenResp = tokenCommonService.userToken(tenantSecretDO, adminUserDto.getId(),adminUserDto.getSn());
        // }
        tokenCommonService.addTenantAuthLog(tenantSecretDO, oauthTokenResp, authTokenClientType);
        oauthTokenResp.setMfaLoginType(adminUserDto.getMfaFlag());
        return CommonResult.success(oauthTokenResp);
    }

}
